/*Program to compute inaction rates*/
/*Will also calculate inaction by size class and "weak" inaction (n +- 1%)*/
/*Revised April 2024*/

libname input "/OEUS/users/FEDSusers/QCEW/data/QuartData/";
libname output "/OEUS/users/FEDSusers/QCEW/revision_2024/Output/";
options  errors=1;

proc delete data=output.inactionrate_allq;
proc delete data=output.inactionrate_allq_wgt;
proc delete data=output.inactionrate_allq_over20;
proc delete data=output.inactionrate_allq_wgt_over20;

%macro mac(YrBeg=,YrEnd=);

%do z=&YrBeg %to &YrEnd;

        %do j=1 %to 4;
                %let qrtyr=&z&j;

	**STOP IF AFTER 2015Q3;
	%if &j>3 and &z=2015 %then %do; %end;
	%else %do;

*x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&z&j..sas7bdat.gz";

data intermed (keep=ldb_num emp year qrt );
                	set input.ldb&z&j;
            
			rename m3_empl=emp;
			
			if own_code~=5 then delete;
			if succ_break_consol_flag~=0 or pred_break_consol_flag~=0 then delete;
			
			
  			qrt=%eval(&j);
                	year=%eval(&z);


			run;

proc sort data=intermed; by ldb_num;

*Merge forward 4 years of data;
%do zz=&z %to &z+4;

        %do jj=1 %to 4;

	    %if &zz=&z and &jj<=&j %then %do; %end;
	    %else %do;
	
                %let qrtyr=&zz&jj;
		
		%if &zz>=2016 %then %do; %end;
		%else %if &zz=2015 and &jj>=4 %then %do; %end;
		%else %do;

*  x gzip -d "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&zz&jj..sas7bdat.gz"; 

proc sort data=input.ldb&zz&jj; by ldb_num;
      data intermed2;
      merge intermed input.ldb&zz&jj (keep=ldb_num m3_empl own_code pred_break_consol_flag 
                		succ_break_consol_flag rename=(m3_empl=emplst) );
      	   by ldb_num;
	   
	   	if own_code~=5 then delete;
		if pred_break_consol_flag~=0 or succ_break_consol_flag~=0 then delete;

  		qrtlst=%eval(&jj);
                yearlst=%eval(&zz);

		
		if emp=0 or emp=. or  emplst=0 or emplst=.  then delete;

		inactive = 0;
		if emp=emplst then inactive=1;
		if inactive = 1 then empavg = 0.5*(emp + emplst);

		*Use 1 emp,1% percent inaction band;
		inactive11 = 0;
		if emp>=min(emplst-1,floor(emplst*.99)) and emp<=max(emplst+1,ceil(emplst*1.01)) then inactive11 = 1;
		if inactive11 = 1 then empavg11 = 0.5*(emp + emplst);
		
		*Use 2 emp, 2% inaction band;
		inactive22 = 0;
		if emp>=min(emplst-2,floor(emplst*.98)) and emp<=max(emplst+2,ceil(emplst*1.02)) then inactive22 = 1;
		if inactive22 = 1 then empavg22 = 0.5*(emp+emplst);
	run;


*  x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&z&j..sas7bdat.gz";
*  x gzip "/OEUS/users/FEDSusers/QCEW/Data/Intermed/ldb&zz&jj..sas7bdat.gz"; 

proc means data=intermed2 noprint nway;
output out=inactionrate mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;
data inactionrate;set inactionrate;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);
proc append base=output.inactionrate_allq data=inactionrate;

****************WEIGHTED;
proc means data=intermed2 noprint nway;
weight emp;
output out=inactionrate_wgt mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;

data inactionrate_wgt;set inactionrate_wgt;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);

proc append base=output.inactionrate_allq_wgt data=inactionrate_wgt;

***************Over 20;
proc means data=intermed2 noprint nway;
where emp>20;
output out=inactionrate_size mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;

data inactionrate_size;set inactionrate_size;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);

proc append base=output.inactionrate_allq_over20 data=inactionrate_size;

***************Over 20 and emp-weighted;
proc means data=intermed2 noprint nway;
where emp>20;
weight emp;
output out=inactionrate_size mean(inactive inactive11 inactive22 empavg empavg11 empavg22)=;

data inactionrate_size_wgt;set inactionrate_size;
qrt=%eval(&j);year=%eval(&z);
qrtlst=%eval(&jj);yearlst=%eval(&zz);

proc append base=output.inactionrate_allq_wgt_over20 data=inactionrate_size_wgt;


run;

%end;
%end;
%end;
%end;
%end;
%end;
%end;
%mend;


*Iterate through all years to compile data, calculate inaction, delete;
%mac(YrBeg=1990,YrEnd=2015);	


